package com.cheboohui.iqs.spider.pingan.price.get;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.cheboohui.iqs.common.basic.TimeUtil;
import com.cheboohui.iqs.common.read.FileUtils;
import com.cheboohui.iqs.common.tool.IDCreate;
import com.cheboohui.iqs.common.unicode.UnicodeParm;
import com.cheboohui.iqs.spider.pingan.entity.C01Duty;
import com.cheboohui.iqs.spider.pingan.price.ComputeCar;
import com.cheboohui.iqs.spring.entity.price.request.PriceRequest;

/**
 * 报价参数参数获取
 * 
 * @author xielonghai
 *
 */
public class Paramten {

	/**
	 * 获取报价请求参数模版
	 * 
	 * @return
	 */
	private static String getDefaultParam() {
		try {
			String res=UnicodeParm.decodeUnicode(
					FileUtils.readFileByChars(new File(FileUtils.getFileUrl_Springboot("json_request_unicode.txt"))));
			res.replace("华瑞0.85", "");
			return res;
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 初始参数获取
	 * 
	 * @return
	 */
	public static JSONObject oneGetPraten(String orderNum, PriceRequest priceRequest, JSONObject obj) {
		JSONObject request = JSONObject.parseObject(getDefaultParam());
		
		request.getJSONArray("aplylicantInfoList").getJSONObject(0).put("invoicePrintType", "05");
		
		request.getJSONObject("saleInfo").getJSONArray("agentInfoList").getJSONObject(0).put("supplementAgreementNo", "8");
		/**2017-07-05 bug修改**/
		request.getJSONObject("saleInfo").getJSONArray("employeeInfoList").getJSONObject(0).put("employeeCode", "2180001456");
		/* 修改--no */
		request.put("mainQuotationNo", orderNum);
		/* 修改--confirmTime */
		request.getJSONArray("quotationList").getJSONObject(0).put("confirmTime", new Date().getTime());

		String vehicleLossInsuredValue = ComputeCar.compute(obj.getJSONObject("car").getString("purchasePrice"),
				priceRequest.getLicense().getRegisteredTime(), obj.getString("busBeginTime"));/* 车辆折旧后的价格 */

		String value = "";
		JSONObject voucher = request.getJSONArray("quotationList").getJSONObject(0).getJSONObject("voucher");
		
		voucher.getJSONObject("applicantInfo").put("invoicePrintType", "05");

		/****交强险相关信息修改***/
		JSONObject c51BaseInfo = voucher.getJSONObject("c51BaseInfo");
		JSONObject calculateResult_c51 = c51BaseInfo.getJSONObject("calculateResult");
		c51BaseInfo.put("supplementAgreementNo", "8");
		c51BaseInfo.put("formatType", "06");
		calculateResult_c51.put("beginTime", TimeUtil.timeNextDay("yyyy-MM-dd") + " 00:00:00");
		calculateResult_c51.put("endTime",
				TimeUtil.timeNextYear(TimeUtil.timeNextDay("yyyy-MM-dd") + " 00:00:00", TimeUtil.DEFAULT_TIME));

		c51BaseInfo.put("insuranceBeginTime", obj.getString("traBeginTime"));
		c51BaseInfo.put("insuranceEndTime",
				TimeUtil.timeNextYear(obj.getString("traBeginTime"), TimeUtil.DEFAULT_TIME));
		if (obj.getBooleanValue("tra")) {
			value = value + "C51";
		} else {
			c51BaseInfo.remove("planCode");
		}

		/****商业险相关信息修改****/
		JSONObject c01BaseInfo = voucher.getJSONObject("c01BaseInfo");
		JSONObject calculateResult_c01 = c01BaseInfo.getJSONObject("calculateResult");
		calculateResult_c01.put("beginTime", TimeUtil.timeNextDay("yyyy-MM-dd") + " 00:00:00");
		calculateResult_c01.put("endTime",
				TimeUtil.timeNextYear(TimeUtil.timeNextDay("yyyy-MM-dd") + " 00:00:00", TimeUtil.DEFAULT_TIME));
		c01BaseInfo.put("insuranceBeginTime", obj.getString("busBeginTime"));
		c01BaseInfo.put("supplementAgreementNo", "8");
		
		c01BaseInfo.put("insuranceEndTime",
				TimeUtil.timeNextYear(obj.getString("busBeginTime"), TimeUtil.DEFAULT_TIME));
		ArrayList<C01Duty> c01DutyList = new ArrayList<C01Duty>();
		if (obj.getBooleanValue("bus")) {
			value = value + "C01";
			c01DutyList = InsuranceDeal.getInsurance(priceRequest.getInsuranceList(), vehicleLossInsuredValue,obj.getJSONObject("car").getIntValue("seats"));
		} else {
			c01BaseInfo.remove("planCode");
		}
		voucher.put("c01DutyList", c01DutyList);

		/* 修改---applyPlans---险别选择（交强险与商业险） */
		request.getJSONArray("quotationList").getJSONObject(0).put("applyPlans", value);

		/** 修改车辆信息 */
		JSONObject vehicleTarget = voucher.getJSONObject("vehicleTarget");
		// 车型编码
		vehicleTarget.put("autoModelCode", obj.getJSONObject("car").getString("autoModelCode"));
		vehicleTarget.put("autoModelName", obj.getJSONObject("car").getString("autoModelName"));
		vehicleTarget.put("brandName", obj.getJSONObject("car").getString("brandName"));
		vehicleTarget.put("brandParaOutYear", obj.getJSONObject("car").getString("firstSaleDate"));
		vehicleTarget.put("circVehicleChineseBrand", obj.getJSONObject("car").getString("brandName"));
		vehicleTarget.put("circVehicleModel", obj.getJSONObject("car").getString("autoModelName"));
		// 发动机号
		if (!(null == priceRequest.getLicense().getEngineNo()) && !"".equals(priceRequest.getLicense().getEngineNo()))
			vehicleTarget.put("engineNo", priceRequest.getLicense().getEngineNo());
		// 排量 >>车船税相关
		vehicleTarget.put("exhaustCapability", obj.getJSONObject("car").getString("exhaustMeasure"));
		// 注册时间
		vehicleTarget.put("firstRegisterDate", priceRequest.getLicense().getRegisteredTime());
		vehicleTarget.put("modifyAutoModelName", obj.getJSONObject("car").getString("autoModelName"));

		vehicleTarget.put("price", obj.getJSONObject("car").getString("displayPrice"));
		vehicleTarget.put("purchasePriceDefaultValue", obj.getJSONObject("car").getString("purchasePrice"));
		// 车架号
		vehicleTarget.put("vehicleFrameNo", priceRequest.getLicense().getFrameNo());
		// 车牌号
		vehicleTarget.put("vehicleLicenceCode", priceRequest.getLicense().getLicenceNo());
		// 车型>>强制险 "A012", 5座 A022 6座以上
		vehicleTarget.put("vehicleTypeCode", obj.getJSONObject("car").getString("vehicleTypeNew"));
		// vehicleSeate--seats
		vehicleTarget.put("vehicleSeats", obj.getJSONObject("car").getString("seats"));
		
		/**新增过户标志--2017-05-18 17：49**/
		if("01".equals(priceRequest.getLicense().getTransfer())){
			vehicleTarget.put("specialCarFlag", "1");
			vehicleTarget.put("changeOwnerFlag", "1");
//			JSONObject cache=new JSONObject();
//			cache.put("brand", "");
//			vehicleTarget.put("cache", cache);
			vehicleTarget.put("transferDate", priceRequest.getLicense().getTransferDate());
		}
		JSONObject cache=new JSONObject();
		cache.put("brand", "");
		vehicleTarget.put("cache", cache);
		
		int seats = obj.getJSONObject("car").getInteger("seats");
		if (seats > 5 && seats < 10) {
			vehicleTarget.put("vehicleClassCode", "1");
			vehicleTarget.put("vehicleTypeName", "六座至十座以下客车");
		}
		// 车辆价格--现在价格
		vehicleTarget.put("vehicleLossInsuredValue", vehicleLossInsuredValue);
		
		/* 修改车主相关信息 */
		Map<String, String> map = IDCreate.getPerson(priceRequest.getOwner().getIdCard(),
				priceRequest.getOwner().getName());
		JSONObject insurantInfo = voucher.getJSONObject("insurantInfo");
		insurantInfo.put("birthday", map.get("birthday"));
		insurantInfo.put("certificateTypeNo", map.get("ID_card"));
		insurantInfo.put("insureSameText", map.get("name") + "(车险-行驶证车主)");
		insurantInfo.put("personnelName", map.get("name"));
		insurantInfo.put("sexCode", map.get("sex"));
		JSONObject ownerDriver = voucher.getJSONObject("ownerDriver");
		ownerDriver.remove("groupCertificateTypeCodeList");
		ownerDriver.put("nationality", "156");
		ownerDriver.put("birthday", map.get("birthday"));
		ownerDriver.put("certificateTypeNo", map.get("ID_card"));
		ownerDriver.put("personnelName", map.get("name"));
		ownerDriver.put("sexCode", map.get("sex"));
		voucher.getJSONObject("vehicleTaxInfo").put("taxPayerId", map.get("ID_card"));

		/* 免税车相关信息修改 */
		if (obj.getBooleanValue("dutyfree")) {
			JSONObject vehicleTaxInfo = voucher.getJSONObject("vehicleTaxInfo");
			vehicleTaxInfo.put("deductionDueCode", obj.getJSONObject("result").getString("deductionDueCode"));
			vehicleTaxInfo.put("deductionDueProportion", obj.getJSONObject("result").getString("rate"));
			vehicleTaxInfo.put("deductionDueType", obj.getJSONObject("result").getString("deductionDueType"));
			vehicleTaxInfo.put("payTaxNo", "250111");
			vehicleTaxInfo.put("taxOrg", "重庆地方税务局");
			vehicleTaxInfo.put("taxType", obj.getJSONObject("result").getString("derateType"));
		}
		return paramUpdate(request);
	}
	
	private static JSONObject paramUpdate(JSONObject request){
		request.remove("insurantInfoList");
		request.getJSONObject("saleInfo").remove("brokerInfoList");
		JSONObject voucher=request.getJSONArray("quotationList").getJSONObject(0).getJSONObject("voucher");
		voucher.remove("accommodationInfoDTO");
		voucher.remove("attachDelayEOAInfo");
		voucher.remove("c01UdwrAttachList");
		voucher.remove("c51FleetInfoDTO");
		voucher.remove("c51UdwrAttachList");
		voucher.remove("propertyList");
		voucher.remove("paymentInfo");
		voucher.remove("saleAgentList");
		voucher.remove("thirdCarBusinessInfoDTO");
		
		voucher.getJSONObject("applicantInfo").remove("groupCertificateTypeCodeList");
		
		JSONObject insurantInfo = voucher.getJSONObject("insurantInfo");
		insurantInfo.remove("birthday");
		insurantInfo.remove("certificateTypeNo");
		insurantInfo.remove("encryptInfoDTO");
		insurantInfo.remove("groupCertificateTypeCodeList");
		insurantInfo.remove("insureSameText");
		insurantInfo.remove("personnelName");
		insurantInfo.put("sameAsText", "--请选择--");
		
		voucher.getJSONObject("saleInfo").put("saleAgentCode", "2180001456");
		
		
		return request;
	}
	
}
